#include "EntityProjectile.h"
#include "WorldModel.h"
#include "ExplosionManager.h"
#include "Sound.h"
CEntityProjectile::CEntityProjectile(int indexSprite, int indexFrame, CVector2D speed, int currentPosX, 
			int currentPosY, int health, int collisionCost, bool active, int side, int offsetSpawnX, 
			int offsetSpawnY): speed(speed), offsetSpawnX(offsetSpawnX), offsetSpawnY(offsetSpawnY)
{
	assert(indexSprite>=0);
	assert(indexFrame>0);

	this->indexSprite=indexSprite;
	this->indexFrame=indexFrame;
	this->currentPosX=currentPosX;
	this->currentPosY=currentPosY;
	this->previousPosX=0;
	this->previousPosY=0;
	this->collisionCost=collisionCost;
	this->active=active;
	this->side=side;
	this->currentHealth=health;	
}

CEntityProjectile::~CEntityProjectile(void){}

/*function that is called to update the entity projectile*/
void CEntityProjectile::update(void)
{
	if(this->currentHealth<=0)
	{
		//if a projectile of the player has touched something he win some points
		if(this->side==eSidePlayer)
		{
			WMODEL.setNumberOfPoint(WMODEL.getNumberOfPoint()+1);
		}
		die();
	}
	else
	{
		this->currentPosX+=(int)speed.getX();
		this->currentPosY+=(int)speed.getY();
		
		//if the entity is out of the screen
		if(this->currentPosX>VIS.getScreenWidth() || this->currentPosX<-VIS.getFrameWidth(this->indexSprite)
			|| this->currentPosY<-VIS.getFrameHeight(this->indexSprite) || this->currentPosY>VIS.getScreenHeight())
		{
			this->active=false;
		}
		//check for collision
		checkCollision();
		if(this->currentHealth<=0)
		{
			//if a projectile of the player has touched something he win some points
			if(this->side==eSidePlayer)
			{
				WMODEL.setNumberOfPoint(WMODEL.getNumberOfPoint()+1);
			}
			die();
		}
		
	}
	
}

/*function called to display the entity*/
void CEntityProjectile::render(void)
{
	VIS.displayFrame(this->indexSprite,this->currentPosX+this->offsetSpawnX,this->currentPosY+this->offsetSpawnY,this->indexFrame,true);
}

/*function called when the entity is dead*/
void CEntityProjectile::die(void)
{
	this->active=false;
	EXPLOSIONMANAGER.spawnExplosion(EXPLOSIONMANAGER.getExplosionTemplate(eIMPACTPROJECTILE),
		this->currentPosX+this->offsetSpawnX, this->currentPosY+this->offsetSpawnY);
	SOUND.stopSound(eSoundImpact);
	SOUND.playSound(eSoundImpact,eVolumeLow);
}

/*function that check if there is a collision and update the health*/
void CEntityProjectile::checkCollision(void)
{
	CWorldEntity* collision=WMODEL.checkCollision(this);
	if(collision!=NULL)
	{
		this->currentHealth-=collision->getCollisionCost();
		collision->setCurrentHealth(collision->getCurrentHealth()-this->collisionCost);
	}
}
int CEntityProjectile::getOffsetSpawnX(void) const
{
	return this->offsetSpawnX;
}
int CEntityProjectile::getOffsetSpawnY(void) const
{
	return this->offsetSpawnY;
}

void CEntityProjectile::setOffsetSpawnX(int offsetX)
{
	this->offsetSpawnX=offsetX;
}
void CEntityProjectile::setOffsetSpawnY(int offsetY)
{
	this->offsetSpawnY=offsetY;
}